﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace PM.PMSys
{
    /// <summary>
    /// 各级管理员的指定等操作
    /// </summary>
    public class setmanager
    {
        public setmanager()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 事务修改管理员（学院管理员、项目管理员）
        /// </summary>
        /// <param name="id">Sys_Guide或Dict_College主键</param>
        /// <param name="tid">教师工号</param>
        /// <param name="tname">教师姓名</param>
        /// <param name="type">修改对象（学院"c"、项目"p"）</param>
        /// <returns>返回受影响行数</returns>
        public int Appointment(int id, string tid, string tname, string type)
        {
            try
            {
                List<string> sqlstr = new List<string>();
                StringBuilder sqlstr1 = null;
                StringBuilder sqlstr2 = null;
                StringBuilder sqlstr3 = null;
                if (type == "p")
                {
                    //因为是一个项目管理员能有管理多个项目，因此换项目管理员是要注意是否他只管一个项目，
                    //若不是的话就不能把他的usertype置为1
                    sqlstr3 = new StringBuilder("update Sys_Account set usertype=1 where tid=(").Append("select inchargeid from Sys_Guide where id=").Append(id).Append(")");
                    sqlstr3.Append(" and (select count(id) from Sys_Guide where inchargeid=(select inchargeid from Sys_Guide where id=").Append(id).Append("))=1");
                    sqlstr1 = new StringBuilder("update Sys_Guide set regisdate=getdate(),incharge='是',inchargeid='").Append(tid).Append("',inchargename='").Append(tname).Append("' ");
                    sqlstr1.Append("where id=").Append(id);
                    sqlstr2 = new StringBuilder("update Sys_Account set usertype=3 where tid='").Append(tid).Append("'");
                }
                else if (type == "c")
                {
                    sqlstr3 = new StringBuilder("update Sys_Account set usertype=1 where tid=(").Append("select inchargeid from Dict_College where collegeid=").Append(id).Append(")");
                    sqlstr1 = new StringBuilder("update Dict_College set regisdate=getdate(),incharge='是',inchargeid='").Append(tid).Append("',inchargename='").Append(tname).Append("' ");
                    sqlstr1.Append("where collegeid=").Append(id);
                    sqlstr2 = new StringBuilder("update Sys_Account set usertype=2 where tid='").Append(tid).Append("'");
                }
                sqlstr.Add(sqlstr3.ToString());
                sqlstr.Add(sqlstr1.ToString());
                sqlstr.Add(sqlstr2.ToString());
                return PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstr);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 事务清理管理员（学院管理员、项目管理员）
        /// </summary>
        /// <param name="id">Sys_Guide主键</param>
        /// <param name="type">修改对象（学院"c"、项目"p"）</param>
        /// <returns>返回受影响行数</returns>
        public int Clear_Manager(int id, string type)
        {
            try
            {
                List<string> sqlstr = new List<string>();
                StringBuilder sqlstr1 = null;
                StringBuilder sqlstr2 = null;
                if (type == "p")
                {
                    sqlstr1 = new StringBuilder("update Sys_Account set usertype=1 where tid=(").Append("select inchargeid from Sys_Guide where id=").Append(id).Append(")");
                    sqlstr1.Append(" and (select count(id) from Sys_Guide where inchargeid=(select inchargeid from Sys_Guide where id=").Append(id).Append("))=1");
                    sqlstr2 = new StringBuilder("update Sys_Guide set regisdate=null,incharge='否',inchargeid=null,inchargename=null where id=").Append(id);
                }
                else if (type == "c")
                {
                    sqlstr1 = new StringBuilder("update Sys_Account set usertype=1 where tid=(").Append("select inchargeid from Dict_College where collegeid=").Append(id).Append(")");
                    sqlstr2 = new StringBuilder("update Dict_College set regisdate=null,incharge='否',inchargeid=null,inchargename=null where collegeid=").Append(id);
                }
                sqlstr.Add(sqlstr1.ToString());
                sqlstr.Add(sqlstr2.ToString());
                return PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstr);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取该学院的注册教师人数并绑定DDL
        /// </summary>
        /// <param name="cname">学院名称</param>
        /// <returns>返回教师数目</returns>
        public int GetCount(string cname,DropDownList ddlcollege)
        {
            SqlParameter[] parameter ={ new SqlParameter("@collegename", SqlDbType.VarChar) };
            parameter[0].Value = cname;
            int sum = 0;//统计datareader读取次数，即数据库记录总数。
            string str = "select tid,[name] from Sys_Account where depart=@collegename and usertype<>3 and usertype<>4 and usertype<>2";
            SqlDataReader reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(str, parameter);
            while (reader.Read())
            {
                ddlcollege.Items.Add(new ListItem(reader["name"].ToString().Trim(),reader["tid"].ToString().Trim()));
                sum += 1;
            }
            reader.Close();
            ddlcollege.Items.Insert(0, new ListItem("选择教师", "0"));
            return sum;
        }
    }
}
